package bookworm;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedList;


public class GetReviewerRatingFromTextFile implements GetReviewersRatingsInterface
{
	/**
	 * it's a hashMap associating reviewers' name with their respective rating. 
	 * Basically, the txt data translated in a hashMap. It will only be construted
	 * one time and when needed. Of course, when the txt changes, by strategy Pattern a new
	 * instance of this class is created and the hashMap reloaded.
	 */
	private HashMap<String, Integer> reviewersNamesAndGradesFromTextFile;

	@Override
	public LinkedList<Integer> getAllReviewersRatings(LinkedList<String> reviewersNames) 
	{
		
		LinkedList<Integer> allReviewersRatings = new LinkedList<Integer>();
		try
		{
			if(reviewersNamesAndGradesFromTextFile == null)
			{
				this.reviewersNamesAndGradesFromTextFile = extractReviewersAndGradesFromText();
			}
			
			for(int i = 0; i < reviewersNames.size(); i++)
			{
				String singleReviewerName = reviewersNames.get(i);
				int singleReviewerRating = this.getReviewerRating(singleReviewerName);
				allReviewersRatings.add(singleReviewerRating);
			}
			
			
			return allReviewersRatings;
		}
		catch(IOException exc)
		{
			System.out.println(exc);
			return allReviewersRatings;
		}
	}
	
	/**
	 * Simply, gets a reviewer's rating
	 * returns -1 if reviewer rating is not found in text file
	 */
	public int getReviewerRating(String reviewerName) 
	{
		try
		{
			if(reviewersNamesAndGradesFromTextFile == null)
			{
				this.reviewersNamesAndGradesFromTextFile = extractReviewersAndGradesFromText();
			}
			
			reviewerName = reviewerName.toLowerCase();
			if(this.reviewersNamesAndGradesFromTextFile.containsKey(reviewerName) == true)
			{
				//there's an associated grade to this reviewer, so let's get it
				int singleReviewerGrade = this.reviewersNamesAndGradesFromTextFile.get(reviewerName);
				return singleReviewerGrade;
			}
			else
			{
				return -1;//reviewer grade not found and it isn't in the text file saved.
			}
		}
		catch(IOException exc)
		{
			return -1;//couldn't extract reviewers and grades from the text File
		}
	}
	
	

	private HashMap<String, Integer> extractReviewersAndGradesFromText() throws FileNotFoundException, UnsupportedEncodingException,IOException 
	{
		FileInputStream inputStream = new FileInputStream("texts/reviewerratings.txt");
		Reader reader = new InputStreamReader(inputStream, "UTF-8");
		BufferedReader bufferedReader = new BufferedReader(reader);
		HashMap<String, Integer> reviewersAndGradesFromTxt = new HashMap<String, Integer>();
		String readLine = bufferedReader.readLine();
		while(readLine != null)
		{
			String [] readLineDivided = readLine.split("==");
			//the first part is the reviewer name, and the second is the reviewer's grade
			String reviewerNameFromTxt = readLineDivided[0];
			int reviewerGradeFromTxt = Integer.valueOf(readLineDivided[1]);
			reviewerNameFromTxt = reviewerNameFromTxt.toLowerCase();
			reviewersAndGradesFromTxt.put(reviewerNameFromTxt, reviewerGradeFromTxt);
			readLine = bufferedReader.readLine();
		}
		
		reader.close();
		bufferedReader.close();
		return reviewersAndGradesFromTxt;
	}


}
